/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.dataImport.r9.bean;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.sys.db.util.ConnectRemoteDB;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.ToolKit;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

@ManagedBean(name = "r9CostDataImportBean")
@SessionScoped
public class DataImport implements Serializable {

    private Integer imonth = 0;
    private List<SelectItem> monthli = new ArrayList<SelectItem>();
    private ConnectRemoteDB dbParam = new ConnectRemoteDB("R9");
    //
    private List<String> updateOrNotli;

    public DataImport() {
        this.updateOrNotli = new ArrayList<String>();
        listMonth();
    }

    public void update() {
        if (updateOrNotli.isEmpty()) {
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "请选择更新内容！");
            return;
        }
        boolean ok = true;
        String orgCode = LoginParam.getOrg().getCode();
        Integer iyear = LoginParam.getIyear();
        try {
            SqlMapClient client = IbatisTemplate.getSqlMapClient();
            Map map = new HashMap();
            map.put("SERVER", dbParam.getSERVER());
            map.put("DB", dbParam.getDB());
            map.put("UID", dbParam.getUID());
            map.put("PWD", dbParam.getPWD());

            map.put("orgCode", orgCode);
            map.put("iyear", iyear);

            client.startTransaction();
            client.startBatch();
            if (updateOrNotli.contains("1")) {
                System.out.println("--> 更新 支出！");
                client.insert("r9DataImport.updateRemoteDataSourceOfR9", map);//更新 支出数据 数据源
            }
            if (updateOrNotli.contains("2")) {
                System.out.println("--> 更新 项目！");
                client.insert("r9DataImport.updateR9FinCostDataConfig", map);//更新 预算项目 配置源 表 数据
            }
            if (updateOrNotli.contains("3")) {
                System.out.println("--> 更新 经济科目！");
                client.insert("r9DataImport.updateR9EcoCostDataConfig", map);//更新 经济科目 配置源 表 数据
            }
            if (updateOrNotli.contains("4")) {
                System.out.println("--> 更新 公用支出！");
                client.insert("r9DataImport.updateR9PubCostKm", map);//公用支出 科目
            }
            if (updateOrNotli.contains("5")) {
                System.out.println("--> 更新 凭证信息！");
                client.insert("r9DataImport.updatePzxx", map);//凭证信息
            }
            client.executeBatch();
            client.commitTransaction();
            client.endTransaction();
        } catch (SQLException ex) {
            ok = false;
            Logger.getLogger(DataImport.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            System.out.println("更新完成!");
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "已更新！");
        } else {
            System.out.println("更新失败!");
            ToolKit.msgs_err(FacesContext.getCurrentInstance(), null, "更新失败！");
        }
    }

    public void updateRemoteDataSoure() {//更新远程数据源 
        boolean ok = true;
        String orgCode = LoginParam.getOrg().getCode();
        Integer iyear = LoginParam.getIyear();
        UpdateDataSource update = new UpdateDataSource();
        try {
            update.updateRemoteDataSource(orgCode, iyear);
        } catch (SQLException ex) {
            System.out.println("更新失败!");
            ok = false;
        }
        if (ok) {
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "已更新！");
            System.out.println("更新完成!");
        }
    }

    private void listMonth() {
        monthli = new ArrayList();
        for (int m = 12; m > 0; m--) {
            Integer str = Integer.valueOf(m);
            monthli.add(new SelectItem(str, str + "月"));
        }
    }

    public void importCostData() {
        String orgCode = LoginParam.getOrg().getCode();
        Integer iyear = LoginParam.getIyear();
        if (orgCode == null || iyear == null || imonth == null) {
            return;
        }
        boolean ok = true;
        Map map = new HashMap();
        map.put("orgCode", orgCode);
        map.put("iyear", iyear);
        map.put("imonth", imonth);
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            client.insert("r9DataImport.updateR9CostDataByProcedure", map);
            client.commitTransaction();
        } catch (SQLException ex) {
            ok = false;
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(DataImport.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(DataImport.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "导入完成");
        } else {
            ToolKit.msgs_err(FacesContext.getCurrentInstance(), null, "导入失败");
        }
    }

    public List<SelectItem> getMonthli() {
        return monthli;
    }

    public void setMonthli(List<SelectItem> monthli) {
        this.monthli = monthli;
    }

    public Integer getImonth() {
        return imonth;
    }

    public void setImonth(Integer imonth) {
        this.imonth = imonth;
    }

    public List<String> getUpdateOrNotli() {
        return updateOrNotli;
    }

    public void setUpdateOrNotli(List<String> updateOrNotli) {
        this.updateOrNotli = updateOrNotli;
    }
}
